package com.chukun.netty.codec;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.time.LocalDateTime;

/**
 * @author chukun
 * 客户端编解码器
 */
public class NettyCodecClientHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
        System.out.println("received server :"+ctx.channel().remoteAddress()+" : "+s);
        ctx.channel().writeAndFlush("send to server"+ctx.channel().remoteAddress()+"-->"+ LocalDateTime.now().toString());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(ctx.channel().remoteAddress()+"上线了");
    }
}
